package Utils;

import org.junit.Test;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.sql.*;
import java.util.Properties;

/**
 * Created by 隼 on 2016/12/23.
 */
public class jdbcUtils {
    private static Properties prop=null;
    private jdbcUtils(){}
    static {
        try {
            prop=new Properties();
            prop.load(new FileReader(URLDecoder.decode(jdbcUtils.class.getClassLoader().getResource("config/db.properties").getPath(),"utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
	 * 获取连接
	 * */

    public static Connection getConn() throws ClassNotFoundException, SQLException {
        //System.out.println(prop.getProperty("jdbc.driver"));
        Class.forName(prop.getProperty("jdbc.driver"));
        System.out.println(prop.getProperty("jdbc.url")+ prop.getProperty("jdbc.username")+prop.getProperty("jdbc.password"));
        return DriverManager.getConnection(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"), prop.getProperty("jdbc.password"));
    }


    //关闭连接
    public static void close(ResultSet rs, Statement stat, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                rs = null;
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                stat = null;
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                conn = null;
            }
        }

    }
}
